﻿@model PTUDW_CNTN2010_F_CDT.Models.UploadModel

<h2>Upload Product</h2>
<h3 style="color:green">@ViewBag.Message</h3>

@using (Html.BeginForm("UploadProduct", "Product", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
    @Html.ValidationSummary();
    <fieldset>
        <legend>Upload Product</legend>
        
        <div class="editor-label">
            @Html.LabelFor(model => model.ProductName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductName)
            @Html.ValidationMessageFor(model => model.ProductName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CategoryId, "CategoryName")
        </div>
        <div class="editor-field">
            @Html.DropDownList("CategoryId", String.Empty)
            @Html.ValidationMessageFor(model => model.CategoryId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Size)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Size)
            @Html.ValidationMessageFor(model => model.Size)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Amount)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Amount)
            @Html.ValidationMessageFor(model => model.Amount)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Warranty)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Warranty)
            @Html.ValidationMessageFor(model => model.Warranty)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Desciption)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Desciption)
            @Html.ValidationMessageFor(model => model.Desciption)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Weight)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Weight)
            @Html.ValidationMessageFor(model => model.Weight)
        </div>

        <div class="editor-label">
            @Html.Label("Model 3D")
        </div>
        <div class="editor-field">
            <input type="file" id="fileToUpload" name="file" />
            <span class="field-validation-error" id="spanfile"></span>
        </div>

        <input type="submit" id="btnSubmit" value="Upload" />
    </fieldset>
}

<script type="text/jscript">
    function GetFileSize(fileid) {
        try {
            var fileSize = 0;
            if ($.browser.msie) {
                var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
                var objFile = objFSO.getFile(filePath);
                var fileSize = objFile.size;
                fileSize = fileSize / 1048576;
            }
            else {
                fileSize = $("#" + fileid)[0].files[0].size
                fileSize = fileSize / 1048576;
            }

            return fileSize;
        }
        catch (e) {
            alert("Error is :" + e);
        }
    }

    function getNameFromPath(strFilepath) {

        var objRE = new RegExp(/([^\/\\]+)$/);
        var strName = objRE.exec(strFilepath);

        if (strName == null) {
            return null;
        }
        else {
            return strName[0];
        }

    }

    $("#btnSubmit").live("click", function () {

        if ($('#fileToUpload').val() == "") {
            $("#spanfile").html("Chọn model file để upload");
            return false;
        }
        else {
            return checkfile();
        }
    });

    function checkfile() {
        var file = getNameFromPath($("#fileToUpload").val());
        if (file != null) {
            var extension = file.substr((file.lastIndexOf('.') + 1));
            switch (extension) {
                case 'jpg':
                case 'png':
                case 'gif':
                case 'pdf':
                case 'exe':
                    flag = true;
                    break;
                default:
                    flag = false;
            }
        }
        if (flag == false) {
            $("#spanfile").text("You can upload only jpg,png,gif,pdf extension file");
            return false;

        }
        else {
            var size = GetFileSize('fileToUpload');
            if (size > 100) {
                $("#spanfile").text("You can upload file up to 3 MB");
                return false;
            }
            else {
                $("#spanfile").text("");
            }
        }
    }
    $(function () {
        $("#fileToUpload").change(function () {
            checkfile();
        });

    });
</script>